package com.wpw.dream.sharding;

import java.util.Collection;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;

/**
 * 分表算法
 * 
 * Project Name：dream-mybatis-sharding-dao
 * ClassName：TbAlgorithm
 * Description：
 * @author: wupengwei
 * @date: 2018年4月4日 上午11:22:41
 * note:
 *
 */
public class TbAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> {  
  
  
    @Override  
    public String doEqualSharding(Collection<String> tableNames, ShardingValue<Integer> shardingValue) {  
        int id = shardingValue.getValue();  
        
        int index = id % 2;  
  
        //sharding-jdbc.xml中配置的 actual-tables：可用的数据表的名称
        for (String each : tableNames) {  
            if (each.endsWith(index + "")) {  
                return each;  
            }  
        }  
        throw new UnsupportedOperationException();  
    }  
  
    @Override  
    public Collection<String> doInSharding(Collection<String> availableTargetNames, ShardingValue<Integer> shardingValue) {  
        return null;  
    }  
  
    @Override  
    public Collection<String> doBetweenSharding(Collection<String> availableTargetNames, ShardingValue<Integer> shardingValue) {  
        return null;  
    }  

}
